﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TuanHuy.BLL;
using TuanHuy.DAL.EntityClasses;

public partial class Admin_CreateProduct : System.Web.UI.Page
{

    private const string pathUpload = "../Uploads/Products";
    private const string pathSave = "Uploads/Products";
    protected void Page_Load(object sender, EventArgs e)
    {
        loadCatolery();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        decimal id=0;
        string location = "";
        string path = "";
        try
        {
            string nextID = Guid.NewGuid().ToString();
            if ((fuImage.PostedFile != null) && (fuImage.PostedFile.ContentLength > 0))
            {
                string fn = System.IO.Path.GetFileName(fuImage.PostedFile.FileName);

                location = Server.MapPath(pathUpload) + "/" + nextID + fn;

                path = pathSave + "/" + nextID + fn;
                try
                {
                    //fImageUpload.PostedFile.SaveAs(location);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
                    int width = 0;
                    if (image.Size.Width > 1000)
                        width = 1000;
                    else
                        width = image.Size.Width;
                    Common.ResizeImage(image, image.Size.Width, location);
                }
                catch (Exception ex)
                {
                    //Response.Write("Error: " + ex.Message);
                }
            }

            ProductsEntity newProduct = new ProductsEntity();
            newProduct.CreateDate = DateTime.Now;
            newProduct = ProductsManager.CreateInstant().Insert(newProduct);
            id = newProduct.ProductId;
            ProductsLanguageEntity pro = new ProductsLanguageEntity();
            pro.ProductId = id;
            pro.CategoryId = int.Parse(ddlCat.SelectedValue);
            pro.LanguageId = 1;
            pro.ProductName = txtProduct.Text;
            pro.Descriptions = txtDescription.Text;
            pro.Price = float.Parse(txtPrice.Text);
            pro.Image = path;
            pro.Content = txtContent.Value;
            ProductsLanguageManager.CreateInstant().Insert(pro);
        }
        catch (Exception ex) { }
    }

    private void loadCatolery()
    {
        ddlCat.DataSource = CategoryManager.CreateInstant().SelectAllLST().ToList();
        ddlCat.DataTextField = "CategoryName";
        ddlCat.DataValueField = "CategoryId";
        ddlCat.DataBind();
        
    }


}